/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Alexandre Croiseaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import marcel.debug.*;
	import marcel.display.Tween;
	import marcel.display.View;
	import marcel.events.StateEvent;
	import marcel.utils.DisplayObjectUtils;
	
	/**
	* @eventType marcel.events.StateEvent.END_UI_OPEN
	*/
	[Event(name = "onEndUIOpen", type = "marcel.events.StateEvent")]
	/**
	* @eventType marcel.events.StateEvent.END_UI_CLOSE
	*/
	[Event(name = "onEndUIClose", type = "marcel.events.StateEvent")]
	
	
	/**
	 * The UI class if the base class for all UIs of a project. When the StateManager creates an UI, 4 methods a called automatically in this order : UI constructor, setParams (if there is params), initialized (after added to stage), and open.
	 * @author Alexandre Croiseaux
	 */
	public class UI extends View
	{
		//--------------------------------------------------------------------------
		//
		//  Public vars
		//
		//--------------------------------------------------------------------------
		/**
		 * Access to the UI data, specified in the config xml file.
		 */
		public var confData:Object;
		/**
		 * Indicates if the UI must be automatically destroyed when setClosed is called.
		 * Destruction includes:
		 * - remove Tweens
		 * - stop MovieClips
		 * - unload and stop Loaders
		 * - remove all children display objects
		 */
		public var autoDestroy:Boolean = true;
		
		
		//--------------------------------------------------------------------------
		//
		//  Private vars
		//
		//--------------------------------------------------------------------------
		/**
		 * @private
		 */
		protected var _isOpened:Boolean;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Constructs a new UI instance
		 */
		public function UI()
		{
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Public methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Opens the UI. This method must be overriden to add fonctionnalities to the UI. setOpened must be called when the UI is completely opened.
		 */
		public function open():void
		{
			setOpened();
		}
		
		/**
		 * Closes the UI. This method must be overriden to add fonctionnalities to the UI. setOpened must be called when the UI is completely closed.
		 */
		public function close():void
		{
			setClosed();
		}
		
		/**
		 * Initialize the UI. This method must be overriden to add fonctionnalities to the UI. This method is called automatically by the state manager when the ui is added to stage.
		 */
		public function initialize():void { }
		
		/**
		 * This method is called by the state manager to update the paramaters of the UI. This method can be called if the current UI is already opened.
		 * This method must be overriden to add fonctionnalities to the UI..
		 */
		public function setParams(params:Object):void { }
		
		/**
		 * Indicates if the current UI is opened or not
		 */
		public function get isOpened():Boolean { return _isOpened; }
		
		/**
		 * Indicates the percentage of a load process if this UI is used as a state loader.
		 * This method does nothing by default and must be overriden to show loading progress.
		 * @param	prc	Loading percentage (0->1)
		 */
		public function setPercentLoaded(prc:Number):void { }
		
		//--------------------------------------------------------------------------
		//
		//  Protected methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Notifies an StateEvent.END_UI_OPEN to the UI listeners.
		 */
		protected function setOpened(e:Event = null):void
		{
			_isOpened = true;
			dispatchEvent(new StateEvent(StateEvent.END_UI_OPEN));
		}
		
		/**
		 * Notifies an StateEvent.END_UI_CLOSE to the UI listeners.
		 */
		protected function setClosed(e:Event = null):void
		{
			_isOpened = false;
			if (autoDestroy) destroy();
			dispatchEvent(new StateEvent(StateEvent.END_UI_CLOSE));
		}
	}

}